package predictionEngine;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Dataset {
    String name;
    List<double[]> instances;
    List<Attribute> attributes = new ArrayList<Attribute>(10);
    
    public Dataset (List<double[]> instances, List<Attribute> attributes){
        this.instances = instances;
        this.attributes = attributes;
    }
    public Dataset(int size){
        instances = new ArrayList<double[]>(size);
    }
    
    public Dataset(double[][] data) {        
        instances = Arrays.asList(data);
    }
    
    public void addInstance(double[] instance){
        instances.add(instance);
    }
    
    public int size(){
        return instances.size();
    }
    
    public void addAttribute(Attribute a){
        attributes.add(a);
    }
    public List<Attribute> getAttributes(){
        return attributes;
    }
    public List<double[]> getInstances(){
        return instances;
    }
    public int targetIndex(){
        return instances.get(0).length-1;
    }
    
    public int getNumberOfAttributes(){
        return attributes.size();
    }
    
    public Attribute getAttribute(int attributeIndex){
        return attributes.get(attributeIndex);
    }
    
    public void sortBy(int attribute){
        Collections.sort(instances,new ArrayComparer(attribute));        
    }
    
    public String getRawData(){
        String s ="";
        for(double[] instance:instances){
            for(int c=0;c<instance.length;c++)
                s+=instance[c] + " \t";
            s+="\n";
        }
        return s;
    }
    
    public Dataset[] split(double testPercent){
        int split = (int) ((1-testPercent)*instances.size());
        List<double[]> trn = instances.subList(0, split);
        List<double[]> tst = instances.subList(split, instances.size());
        Dataset train = new Dataset(trn,attributes);
        Dataset test = new Dataset(tst,attributes);
        Dataset[] datasets = {train,test};//new Dataset[2];
        return datasets;        

    }
    
    
    public Dataset[] split(ISplit split){
        List<double[]> lft = new ArrayList<double[]>(instances.size());
        List<double[]> rght = new ArrayList<double[]>(instances.size());
        for(double[] instance:instances){
            if(split.test(instance))
                lft.add(instance);
            else
                rght.add(instance);
        }
        Dataset left = new Dataset(lft,attributes);
        Dataset right = new Dataset(rght,attributes);
        Dataset[] datasets = {left,right};//new Dataset[2];
        return datasets;
    }            

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
